package com.berrontech.asbog;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import java.util.Objects;

/**
 * Create by levent8421 2021/4/8 17:53
 * MainActivity
 * Main activity
 *
 * @author levent8421
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String AUTO_START = "auto_start";
    private static final String TAG = "MainActivity";
    private static final String SEARCH_DELIMITER = "?";

    private EditText etPrefix;
    private EditText etUrl;
    private SwitchCompat swDisplayReverse;

    private ApplicationConfig config;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        try {
            config = ApplicationConfig.loadFromDisk(this);
        } catch (Exception e) {
            Toast.makeText(this,
                    "Can not load config:" + e.getClass().getName() + e.getMessage(),
                    Toast.LENGTH_LONG)
                    .show();
            return;
        }
        setSavedValue();
        final boolean autoStart = getIntent().getBooleanExtra(AUTO_START, false);
        if (autoStart) {
            handleBtnEnterClick();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        showNavigation();
    }

    private void showNavigation() {
        try {
            final int res = NavigationBarUtils.showNavigation(this);
            if (res < 0) {
                throw new Exception("Cmd res code=" + res);
            }
        } catch (Exception e) {
            Toast.makeText(this, "Can not show navigation for this device:" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    private void setSavedValue() {
        etUrl.setText(config.getUrl());
        etPrefix.setText(config.getPrefix());
        swDisplayReverse.setChecked(orFalse(config.getDisplayReverse()));
    }

    private void initView() {
        etPrefix = findViewById(R.id.etPrefix);
        etUrl = findViewById(R.id.etUrl);
        swDisplayReverse = findViewById(R.id.swDisplayReverse);
        findViewById(R.id.btnEnter).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (Objects.equals(v.getId(), R.id.btnEnter)) {
            handleBtnEnterClick();
        }
    }

    private void handleBtnEnterClick() {
        if (!saveConfig()) {
            return;
        }
        final Intent intent = new Intent(this, DashboardActivity.class);
        final String url = buildUrl();
        Log.d(TAG, "URL=" + url);
        intent.putExtra(DashboardActivity.WEB_VIEW_URL, url);
        startActivity(intent);
    }

    private String buildUrl() {
        final StringBuilder sb = new StringBuilder(config.getUrl());
        if (sb.indexOf(SEARCH_DELIMITER) < 0) {
            sb.append('?');
        }
        sb.append("prefix=").append(config.getPrefix())
                .append("&reverse=").append(orFalse(config.getDisplayReverse()));
        return sb.toString();
    }

    private boolean orFalse(Boolean b) {
        return b == null ? false : b;
    }


    private boolean saveConfig() {
        config.setPrefix(etPrefix.getText().toString());
        config.setUrl(etUrl.getText().toString());
        config.setDisplayReverse(swDisplayReverse.isChecked());
        try {
            config.save();
        } catch (Exception e) {
            Toast.makeText(this,
                    "Can not save config:" + e.getClass().getName() + e.getMessage(),
                    Toast.LENGTH_LONG)
                    .show();
            return false;
        }
        return true;
    }
}